package org.example.config;

/**
 * @description:
 * @Author: whopxx
 * @CreateTime: 2025-07-27
 */

import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.example.config.i18nconfig.I18nQueryInterceptor;
import org.example.config.i18nconfig.I18nResultInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

/**
 * MyBatis国际化配置
 *
 * 配置MyBatis的Interceptor以支持自动i18n翻译
 */
@Slf4j
@Configuration
public class MybatisI18nConfig {

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    @Autowired
    private I18nResultInterceptor i18nResultInterceptor;

    @Autowired
    private I18nQueryInterceptor i18nQueryInterceptor;


    @PostConstruct
    public void configureMybatis() {
        org.apache.ibatis.session.Configuration configuration = sqlSessionFactory.getConfiguration();


        // 注册结果翻译拦截器
        configuration.addInterceptor(i18nResultInterceptor);

        // 注册查询参数反向翻译拦截器
        configuration.addInterceptor(i18nQueryInterceptor);

        log.info("MyBatis国际化配置完成: 拦截器已注册");
    }
}